<?php

class ErrorController extends Agitum_Controller_Action
{
	protected $_log = null;
	
	public function getLog() {
		if ( !$this->_log ) {
	        $bootstrap = $this->getInvokeArg('bootstrap');
	        if ( $bootstrap->hasResource('log') ) {
	        	$this->_log = $bootstrap->getResource('log');
	        }
		}
        
        return $this->_log;
	}
	
	public function errorAction()
    {
        $errors = $this->_getParam('error_handler');
        switch ($errors->type) {
            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ROUTE:
            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
                // 404 error -- controller or action not found
                $this->getResponse()->setHttpResponseCode(404);
                $this->view->message = 'Page not found';
                break;
//            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_OTHER: 
//            	$this->getResponse()->setHttpResponseCode($errors->exception->getCode());
//            	$this->view->message = $errors->exception->getMessage();
//            	break;
            default:
                // application error
                $this->getResponse()->setHttpResponseCode(500);
                $this->view->message = 'Application error';
                break;
        }
        
        // Log exception, if logger available
        $log = $this->getLog();
        if ( $log ) {
        	$msg = $this->view->message .': '. $this->getRequest()->getRequestUri();
            $log->crit($msg, $errors->exception);
        }
        
        // conditionally display exceptions
        if ($this->getInvokeArg('displayExceptions') == true) {
            $this->view->exception = $errors->exception;
        }
        
        $errors->request->setDispatched(false);
        $this->view->request = $errors->request;
    }
    
    public function accessAction() {}

}

